package com.le.tester.leetcode;

import javax.xml.stream.events.Characters;
import java.util.ArrayDeque;

/**
 * createTime：2022/6/15 9:58
 * description： 删除字符串中的所有相邻重复项
 */
public class RemoveDuplicates {
    public String removeDuplicates(String s) {
        int length = s.length();
        ArrayDeque<Character> deque = new ArrayDeque<>();
        char ch;
        for (int i = 0; i < length; i++) {

            ch = s.charAt(i);

            if (deque.isEmpty() || !deque.peek().equals(ch)) {
                deque.push(ch);
            } else {
                //取出顶部元素，返回底部第一个元素
                deque.pop();
            }
        }
        StringBuilder result = new StringBuilder();
        //Character firstCh = deque.removeFirst();
        //Character lastCh = deque.removeLast();
        while (!deque.isEmpty()) {
            //返回移除的元素
            result.append(deque.pollLast());
        }
        return result.toString();
    }

    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("abbaca");
        System.out.println(new RemoveDuplicates().removeDuplicates(sb.toString()));
    }
}
